﻿<%@ Page Title="岗位管理" Language="C#" MasterPageFile="~/master/m.master" %>
<script runat="server"></script>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<link href="../assets/css/plugins/jsTree/style.min.css" rel="stylesheet">
<link href="../assets/css/plugins/footable/footable.core.css" rel="stylesheet">
<link href="../assets/css/plugins/iCheck/custom.css" rel="stylesheet">
<link href="../assets/img/validate/validate.css" rel="stylesheet">
<style type="text/css">
@media (min-width: 768px) {
	.modal-dialog {
		width: 768px;
		margin: 30px auto;
	}
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="main" Runat="Server">
	<div class="system-box">
		<div class="left-side  ibox-content">
			<div id="using_json"></div>
		</div>
		<div class="right-side">
			<div class="btns-container">
				<div class="modal inmodal" id="addWindow" tabindex="-1" role="dialog" aria-hidden="true">
					<form id="checkModuleForm" method="get" action="#">
					<div class="modal-dialog">
						<div class="modal-content animated bounceInRight">
							<div class="modal-header">
								<span class="editTitle">新增岗位</span>
								<button type="button" class="close" data-dismiss="modal">
									<span aria-hidden="true">&times;</span>
									<span class="sr-only">关闭</span>
                                </button>
							</div>
							<div id="message" style="display: none"></div>
							<div class="modal-body">
								<table class="module-info" cellpadding="0" cellspacing="0">
									<thead></thead>
									<tbody>
										<tr>
											<td colspan="2">
												<div class="form-group">
													<label class="control-label">岗位名称&nbsp;&nbsp;</label>
													<input id="postName" name="postName" placeholder="请输入岗位名称" type="text"
														datacol="yes" err="岗位名称" checkexpession="NotNull"
														maxlength="15" class="form-control input-w660 required" />
												</div>
											</td>
										</tr>
										<tr>
											<td colspan="2">
												<div class="form-group">
													<label class="control-label col-sp">是否有效&nbsp;&nbsp;</label>
													<div class="checkbox i-checks label-inline">
														<label class="effective">
															<input id="isEffective" type="checkbox" checked="">
														</label>
	                                        		</div>
                                                    <div class="label-text label-inline">
	                                                        <i class="fa fa-lightbulb-o "></i> 注意：禁用该岗位后，将不能被使用。
	                                                    </div>
                                        		</div>
											</td>
										</tr>
										<tr>
											<td colspan="2">
												<div class="form-group">
													<label class="control-label label-float">备注说明&nbsp;&nbsp;</label>
													<textarea id="memo" style="margin-left: 3px;" placeholder="在此输入备注说明" 
														name="memo" class="form-control input-textarea input-w660"></textarea>
												</div>
											</td>
										</tr>
									</tbody>
								</table>
							</div>
							<div class="modal-footer">
								<button type="button" id="addModuleBtn" class="btn btn-primary">保存</button>
								<button type="button" id="closeAddBox" class="btn btn-white" data-dismiss="modal">关闭</button>
							</div>
						</div>
					</div>
					</form>
				</div>
			</div>
			
			<div class="right-content-box">
				<div class="ibox-content">
					<table class="footable table table-stripped table-list">
						<thead></thead>
						<tbody id="pageList"></tbody>
					</table>
                 </div>
			</div>
             
		</div>
	</div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="footer" Runat="Server">
<script src="../assets/js/plugins/jsTree/jstree.min.js"></script>
<script src="../assets/js/plugins/footable/footable.all.min.js"></script>
<script src="../assets/js/plugins/iCheck/icheck.min.js"></script>
<script src="../assets/js/page/dept.js"></script>
<script src="../assets/js/page/rScroll.js"></script>
<script src="../assets/js/page/tableControl.js"></script>
<script src="../assets/img/validate/learunui-validator.js"></script>
<script type="application/javascript">	
$(document).ready(function() {
    //初始化表格
    //$('.footable').footable();
	
    //初始化多选框
    $(".i-checks").iCheck({
        checkboxClass:"icheckbox_square-green",
    });
	
	//限制textarea输入字数
	commonUtil.inputLimitForId('memo',50);
	
    //用于记录当前选中节点id（部门id）
    var deptId = 1;
    //用于记录当前选中节点grade
    var deptGrade = 1;
    //用于给予当前选中节点父id
    var deptParent = 0;
    //用于标记是新增还是编辑
    var isCreate = true;
    //用于记录选中的岗位id
    var postId = 0;
    //模块ID
    var ModuleID = Y.getUrlParam("ModuleID");
    //初始树形列表
	var initTree = true;
    //加载导航列表
    var loadList = function(init) {
        //调用加载层
        layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
      	initTree = true;
        var postData = {
            DeptCode:1
        }
        Y.API2('Dept', 'GetList', postData,function(data) {
            //关闭加载层
            layer.closeAll('dialog');
			
            var treeData = {};
            if(!Y.checkNull(data) && data.length > 0){
                //json处理
                data.map(function(item,i){
                    treeData.text = item.DeptContent;
                    treeData.data = {
                        Grade: item.Grade,
                        DeptID: item.DeptID,
                        ParentID: item.ParentID,
                    };
                    if(item.Children.length > 0){
                        treeData.state = {opened: true};
                        treeData.children = deptUtils.getChildNode(item.Children);
                    }
                })
				
                postId = 0;
                //禁用删除、修改按钮
                $('#editBtn,#deleteBtn').attr('disabled','disabled');
				
                //初始化左侧菜单
                $.jstree.destroy ("#using_json");
                $('#using_json').jstree({
                    core: {
                        data: [treeData]
                    }
                }).bind('select_node.jstree', function(event, data) {
                	initTree = false;
                    //绑定的点击事件
                    var node_data = data.node.data;
                    deptId = node_data.DeptID;
                    deptGrade = node_data.Grade;
                    deptParent = node_data.ParentID;
                    postId = 0;
                    //禁用删除、修改按钮
                    $('#editBtn,#deleteBtn').attr('disabled','disabled');
                    //加载右侧列表
                    loadPageList(deptId);
                });
				
                //判断是否为初次调用
                if(!Y.checkNull(init)){
                    //获取初始化的相关属性
                    deptId = data[0]['DeptID'];
                    deptGrade = data[0]['Grade'];
                    deptParent = data[0]['ParentID'];

                    //加载右侧列表
                    loadPageList(deptId);
                }
            }
        });
    }
    loadList(true);
	
    //选中岗位
    $('#pageList').on('click','tr',function(){
        var pid = $(this).find('td:first').text();
        if(postId == parseInt(pid))
            return false;
        postId = parseInt(pid);
        if(!isNaN(postId))
        	$('#editBtn,#deleteBtn').removeAttr('disabled');
        $('.footable tr').removeClass('check');
        $(this).addClass('check');
    });
	
    //获取岗位详情
    var loadDetail = function(postId){
        layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
        Y.API2('Post','GetDetails',{PostID:postId},function(data){
            layer.closeAll('dialog');
            if(data.Success == false){
                swal(data.Message, '', 'error');
            }else{
                $('#postName').val(data.PostName);
                $('#memo').val(data.Memo || '');
                if($('#isEffective').is(':checked') != data.IsEffective){
                    $('#isEffective').parent().click();
                }
            }
        });
    }
	
    //获取右侧内容列表
    var loadPageList = function(deptId){
    	if($('.footable thead').children().size() == 0)
    		tableControl.initThead2('.footable',['编号','岗位名称','备注','状态']);
    	
        Y.API2('Post', 'GetList', { DeptID: deptId },function (data) {
            if(data.Success == false){
                swal(data.Message, '', 'error');
            }else{
                postId = 0;
                $('#editBtn,#deleteBtn').attr('disabled','disabled');
                var tBody = [];
                if (!Y.checkNull(data)) {
                	data.map(function(item,i){             		
						var tempTr = {
							trClass: (i % 2 == 0 ? 'gradeX footable-even' : 'gradeX footable-odd' ),
							children: [
								{tdText: item.PostID, c:0 , r:0, tdHtml:'', tdId:'', tdClass:''},
								{tdText: (item.PostName || ' '), c:0 , r:0, tdHtml:'', tdId:'', tdClass:''},
								{tdText: (item.Memo || ' '), c:0 , r:0, tdHtml:'', tdId:'', tdClass:''},
								{tdText: (item.IsEffective ? ' 有效' : ' 无效'), c:0 , r:0, tdHtml: ('<i class="fa fa-'+ (item.IsEffective ? 'check text-navy' : 'times text-danger') +'"></i>'), tdId:'', tdClass:''},
							]
						};
						tBody.push(tempTr);
					})
                }
                tableControl.initTbody('.footable',(tBody.length > 0 ? tBody : null),3,null);
            }
        });
    }

    var GetBtnJson = function (url, data, callBack) {
        $.ajax({
            type: "Get",
            url: url,
            data: data,
            async: false,
            dataType: 'json',
            success: function (result) {
                callBack(result);
            }
        });
    }

	commonUtil.initDeleteBtnEvent({
	    callBack: function () {
	        Y.API2('Post', 'Remove', { ID: postId }, function (data) {
	            layer.closeAll('dialog');
	            if (data.Success == false) {
	                swal('删除失败', data.Message, 'error');
	            } else {
	                swal('删除成功', '该信息已被你永久删除', 'success');
	                loadPageList(deptId);
	            }
	        });
	    }
	});
	
    //获取按钮
	commonUtil.loadBtnList(ModuleID,function(){
		rScroll();
	});
	
	//刷新按钮
	$('.btns-container').on('click', '#refreshBtn', function () {
	    loadList();
	    loadPageList(deptId);
	});
	//点击删除按钮
	$('.btns-container').on('click', '#deleteBtn', function () {
	    deletePost(postId);
	});
	//新增按钮
	$('.btns-container').on('click', '#addBtn', function () {
		if(initTree){
			swal('请先在目录树选择节点');
			return false;
		}
	    $('.editTitle').text('新增岗位');
	    isCreate = true;
	    $(':input', '#checkModuleForm').not(':button, :submit, :reset').val('');
	    if (!$('#isEffective').is(':checked'))
	        $('#isEffective').parent().click();
	});
	//编辑按钮
	$('.btns-container').on('click', '#editBtn', function () {
	    $('.editTitle').text('编辑岗位');
	    isCreate = false;
	    loadDetail(postId);
	});

	//检验提交信息
	$('#addModuleBtn').click(function(){
		if (!CheckDataValid('#checkModuleForm')) {
            return false;
        }
		//检验通过后执行新增或者编辑
		var deal = '';
		var successText = '';
		if(isCreate){
			deal = 'Create';
			successText = '新增成功';
		}else{
			deal = 'Modify';
			successText = '编辑成功';
		}
		layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
		Y.API2('Post', deal, {
			PostID: postId,
			PostName: $('#postName').val(),
			DeptID: deptId,
			IsEffective: $('#isEffective').is(':checked'),
			Memo: $('#memo').val()
		},function(data){
			layer.closeAll('dialog');
			if(data.Success == false){
				swal(data.Message, '', 'error');
			}else{
				swal(successText, '', 'success');
				//关闭新建  or 编辑框
				$('#closeAddBox').click();
				//刷新列表
				loadPageList(deptId);
			}
		})
	})
});
</script>
</asp:Content>